package com.ruoyi.web.controller.iot;

import java.util.HashMap;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.iot.domain.bo.EnvQueryHistoryBo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.iot.domain.IotEnv;
import com.ruoyi.iot.service.IIotEnvService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * 扬尘设备Controller
 *
 * @author 陈江灿
 * @date 2025-02-13
 */
@RestController
@RequestMapping("/iot/env")
public class IotEnvController extends BaseController {
    @Autowired
    private IIotEnvService iotEnvService;

    @Value("${env.url}")
    private String serverUrl;

    @Value("${env.historyUrl}")
    private String historyUrl;

    /**
     * 查询扬尘设备历史记录
     * @return
     */
    @PreAuthorize("@ss.hasPermi('iot:env:queryHistory')")
    @GetMapping("/queryHistory")
    public AjaxResult queryHistory(EnvQueryHistoryBo bo) {
        HttpResponse response = HttpRequest.get(historyUrl)
                .form("uid",  bo.getUid())
                .form("date",  bo.getDate())
                .form("startTime",  bo.getStartTime())
                .form("stopTime",  bo.getStopTime())
                .execute();
        String responseBody = response.body();
        if (StrUtil.isEmpty(responseBody)) {
            return error("服务器响应为空");
        }
        JSONObject entries = JSONUtil.parseObj(responseBody);
        return success(entries);
    }

    /**
     * 获取佳佳眼设备详情
     * @param envId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('iot:env:query')")
    @GetMapping(value = "/getEnvInfo/{envId}")
    public AjaxResult getEnvInfo(@PathVariable("envId") Long envId) {
        IotEnv iotEnv = iotEnvService.selectIotEnvByEnvId(envId);
        HttpResponse response = HttpRequest.get(serverUrl)
                .form("uid", iotEnv.getSensorId())
                .execute();
        String responseBody = response.body();
        if (StrUtil.isEmpty(responseBody)) {
            return error("服务器响应为空");
        }
        JSONObject entries = JSONUtil.parseObj(responseBody);
        return success(entries);
    }

    /**
     * 查询扬尘设备列表
     */
    @PreAuthorize("@ss.hasPermi('iot:env:list')")
    @GetMapping("/list")
    public TableDataInfo list(IotEnv iotEnv) {
        startPage();
        List<IotEnv> list = iotEnvService.selectIotEnvList(iotEnv);
        return getDataTable(list);
    }

    /**
     * 导出扬尘设备列表
     */
    @PreAuthorize("@ss.hasPermi('iot:env:export')")
    @Log(title = "扬尘设备", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotEnv iotEnv) {
        List<IotEnv> list = iotEnvService.selectIotEnvList(iotEnv);
        ExcelUtil<IotEnv> util = new ExcelUtil<IotEnv>(IotEnv.class);
        util.exportExcel(response, list, "扬尘设备数据");
    }

    /**
     * 获取扬尘设备详细信息
     */
    @PreAuthorize("@ss.hasPermi('iot:env:query')")
    @GetMapping(value = "/{envId}")
    public AjaxResult getInfo(@PathVariable("envId") Long envId) {
        return success(iotEnvService.selectIotEnvByEnvId(envId));
    }

    /**
     * 新增扬尘设备
     */
    @PreAuthorize("@ss.hasPermi('iot:env:add')")
    @Log(title = "扬尘设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotEnv iotEnv) {
        return toAjax(iotEnvService.insertIotEnv(iotEnv));
    }

    /**
     * 修改扬尘设备
     */
    @PreAuthorize("@ss.hasPermi('iot:env:edit')")
    @Log(title = "扬尘设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotEnv iotEnv) {
        return toAjax(iotEnvService.updateIotEnv(iotEnv));
    }

    /**
     * 删除扬尘设备
     */
    @PreAuthorize("@ss.hasPermi('iot:env:remove')")
    @Log(title = "扬尘设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/{envIds}")
    public AjaxResult remove(@PathVariable Long[] envIds) {
        return toAjax(iotEnvService.deleteIotEnvByEnvIds(envIds));
    }
}
